import Taro from '@tarojs/taro';
import { useState, useEffect } from "react";
import { View, Button } from '@tarojs/components';
import { AtForm, AtInput } from 'taro-ui';
import "taro-ui/dist/style/components/form.scss";
import "taro-ui/dist/style/components/button.scss";
import "taro-ui/dist/style/components/input.scss";
import "taro-ui/dist/style/components/radio.scss";
import { updateUserInfo } from '@/api/my';
import css from './index.module.less';

export default function MyInfo() {
  const [userInfo, setUserInfo] = useState({});
  useEffect(() => {
    Taro.getStorage({
      key: 'userInfo',
      success: function (a) {
        setUserInfo(a.data);
      }
    });
  }, []);
  const ok = (event) => {
    const id = userInfo.partyId;
    const value = event[0].detail.value;
    updateUserInfo({ id: id, name: value.name, telNumber: value.telNumber }).then((res) => {
      if (res.code === 200) {
        Taro.showToast({
          title: '修改成功',
          duration: 500,
          icon: 'success',
          complete() {
            setTimeout(Taro.navigateBack, 500);
          }
        });
      } else {
        Taro.showToast({
          title: '修改失败',
          duration: 500,
          icon: 'error',
        });
      }
    }).catch(() => {
      Taro.showToast({
        title: '修改失败',
        duration: 500,
        icon: 'error',
      });
    });
  };
  return (
    <View className={css.app}>
      <AtForm
        onSubmit={ok}
      >
        <AtInput
          name='type'
          title='类别'
          type='text'
          value={userInfo.type === 0 ? '学生' : userInfo.type === 1 ? '教师' : '无'}
          disabled
        />
        <AtInput
          name='identity'
          title='身份'
          type='text'
          value={userInfo.identity === 0 ? '积极分子' : userInfo.identity === 1 ? '预备党员' : userInfo.identity === 2 ? '党员' : userInfo.identity === 3 ? '书记' : '无'}
          disabled
        />
        <AtInput
          name='name'
          title='姓名'
          type='text'
          placeholder='请输入姓名'
          value={userInfo.name}
        />
        <AtInput
          name='studentJobNumber'
          title='工号/学号'
          type='text'
          value={userInfo.studentJobNumber}
          disabled
        />
        <AtInput
          name='organizationName'
          title='学院'
          type='text'
          value={userInfo.organizationName}
          disabled
        />
        <AtInput
          name='higherOrgName'
          title='所属上级'
          type='text'
          value={userInfo.higherOrgName}
          disabled
        />
        <AtInput
          name='positionName'
          title='职务'
          type='text'
          value={userInfo.positionName}
          disabled
        />
        <AtInput
          name='telNumber'
          title='联系方式'
          type='text'
          placeholder='请输入联系方式'
          value={userInfo.telNumber}
        />
        <Button circle formType='submit' type='primary' className={css.botton}>提交</Button>
      </AtForm>
    </View>
  );
}